package com.app.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;

import ReadWriteXML.CNeighbor;
import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import app.com.ModuleRole.R;

public class frmDanhSachOnline extends Activity {
	ListView lvDanhSachThanhVien;
	String[] data;
	ArrayList<CNeighbor> listNeighbor;
	Button btnMoiVaoNhom;
	String groupName = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_danh_sach_online);
		lvDanhSachThanhVien = (ListView) findViewById(R.id.lvDanhSachThanhVien);
		btnMoiVaoNhom = (Button) findViewById(R.id.btnMoiVaoNhom);
		lvDanhSachThanhVien.setChoiceMode(2);
		listNeighbor = ReadWriteFileController.readNeighbor();
		fillListView();
		registerForContextMenu(lvDanhSachThanhVien);
		btnMoiVaoNhom.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SparseBooleanArray checkedList = lvDanhSachThanhVien.getCheckedItemPositions();
				String s = "";
				for (int i = 0; i < checkedList.size(); i++) {
					NetworkController.sendGroupInvite(new Address(listNeighbor.get(i).get_member().get_ip()),
							groupName, "Vào nhóm mình nhé");
					s += listNeighbor.get(i).get_member().get_ip() + " ";
				}
				Toast.makeText(getBaseContext(), s, 1).show();
				//fillListView();
				
				finish();
			}
		});
		// Lấy tên nhóm từ activity trước
		Intent i=getIntent();		
		if(i!=null){
			if(i.getStringExtra(frmTaoNhom.CODE_GROUP_NAME)!=null)
				groupName=i.getStringExtra(frmTaoNhom.CODE_GROUP_NAME);
		}
	}

	private void fillListView() {

		
		ArrayAdapter<CNeighbor> adapter = new ArrayAdapter<CNeighbor>(getBaseContext(),
				android.R.layout.simple_list_item_checked, listNeighbor);
		lvDanhSachThanhVien.setAdapter(adapter);
	}
	
}
